package com.lottery.material.service.portal.user.impl;

import com.lottery.material.dao.portaluser.CollectInfoMapper;
import com.lottery.material.pojo.portaluser.CollectInfo;
import com.lottery.material.service.portal.user.CollectInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * Description:    收藏信息服务实现类
 * Author:         sunny
 * CreateDate:     2019-04-24 11:01
 */
@Service
@Slf4j
public class CollectInfoServiceImpl implements CollectInfoService {

    @Autowired
    private CollectInfoMapper collectInfoMapper;

    /**
     * description: 根据用户id和文章id查询是否有收藏记录
     * author sunny
     * createDate 2019-04-24 上午 11:01
     *
     * @param userId
     * @param articleId
     * @return
     */
    @Override
    public CollectInfo findByUserIdAndArticleId(String userId, String articleId) {
        return collectInfoMapper.findByUserIdAndArticleId(userId, articleId);
    }

    /**
     * description: 删除收藏文章
     * author sunny
     * createDate 2019-04-26 下午 2:28
     * @param collectInfo
     * @return
     */
    @Override
    public Integer deleteCollectInfo(CollectInfo collectInfo) {
        CollectInfo collect = collectInfoMapper.findByUserIdAndArticleId(collectInfo.getUserId(), collectInfo.getArticleId());
        if (Objects.isNull(collect)) {
            log.info("当前用户：{}-->没有查询到收藏该文章{}记录；", collectInfo.getUserId(), collectInfo.getArticleId());
            return -1;
        }

        Integer count = collectInfoMapper.deleteByPrimaryKey(collect.getId());
        if (count <= 0) {
            return -1;
        }
        return count;
    }

    /**
     * description: 保存收藏文章
     * author sunny
     * createDate 2019-04-26 下午 2:35
     * @param collectInfo
     * @return
     */
    @Override
    public Integer saveCollectInfo(CollectInfo collectInfo) {
        CollectInfo collect = collectInfoMapper.findByUserIdAndArticleId(collectInfo.getUserId(), collectInfo.getArticleId());
        if (Objects.nonNull(collect)) {
            log.info("当前用户：{}-->已经收藏过该文章{}", collectInfo.getUserId(), collectInfo.getArticleId());
            return -1;
        }

        Integer count = collectInfoMapper.insertSelective(collectInfo);
        if (count <= 0) {
            return -1;
        }
        return count;
    }
}
